package com.study.controller;


import com.study.domain.SMSCode;
import com.study.service.SMSCodeService;
import com.study.utils.Sample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;


//@RestController
@Controller
public class SMSCodeController {
    //Logger logger = Logger.getLogger("SMSCodeController");
    //
    @Autowired
    private SMSCodeService smsCodeService;
    @Autowired
    private Sample sample;

    @RequestMapping("/sms")
    @ResponseBody
    public String getCode(String tele) throws Exception {
        //logger.info("telelogger=>"+tele);
        //
        System.out.println("tele=>" + tele);
        String code = smsCodeService.sendCodeToSMS(tele);
        String str="暂时关闭阿里云短信";
        //上面是模拟发送，只能控制台收到，下面要开始发送短信了,调用Sample工程代码,,处理异常。下面这行注释，就等于关闭阿里云
        // str = sample.sendCodeAli(tele, code);
        System.out.println("Sample工程代码 发送短信后的json响应信息," + str);
        return code;
    }

    //思考如何校验？发送什么请求，点击登录的时候进来
    @RequestMapping("/logintel")
    public String checkCode(Model model, HttpServletRequest request) {
        System.out.println("logintel进入");
        String tele = request.getParameter("tele");
        String code = request.getParameter("code");

        SMSCode smsCode = new SMSCode();

        smsCode.setTele(tele);
        smsCode.setCode(code);
        System.out.println("进入logintel 的checkCode，开始校验");

        boolean b = smsCodeService.checkCode(smsCode);

        if (b) {
            model.addAttribute("tele", tele);
            return "index0";
        } else {
            String msg = "手机验证码错误，请重新输入";
            model.addAttribute("msg", msg);
            //model.addAttribute("currentYear", Calendar.getInstance().get(Calendar.YEAR));
            //
            return "login";
        }

        //todo 此处不再演示整合mybatis的登录，同学们自行整合

        //String username = request.getParameter("username");
        //String password = request.getParameter("password");
        //
        //System.out.println("username=>"+username);
        ///*Todo 从这里开始去连接数据库校验*/
        //User2 user2 = user2Mapper.findByNameAndPassword(username, password);
        //System.out.println("user2=>"+user2);
        //if (user2 == null) {
        //    //    登录失败，给用户提示
        //    String msg = "用户名或者密码错误，请重新输入";
        //    model.addAttribute("msg", msg);
        //    return "login";
        //} else {
        //    //   成功就跳转
        //    model.addAttribute("username", username);
        //    return "index";
        //}


    }


}
